layui.use(['form','layer','jquery','table','laydate'],function() {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        table = layui.table,
        laydate = layui.laydate;

    var tableData;
    //加载页面日期控件
    laydate.render({
        elem: '#month'
        ,format: 'yyyy-MM' //可任意组合
        ,type: 'month'
        ,ready: function (date) {
            $(".layui-laydate").on('click', 'ul li', function () {
                $(".layui-laydate").remove();
            });
        }
        ,change: function (value) {
            $("#month").val(value);
        }
        ,done: function (value){
            var shadeIndex = top.layer.msg('数据加载中，请稍候', {icon: 16, time: false, shade: 0.4});
            table.reload('table',{
                url: '/manage/elements_price/list/'+value
                ,limit: 999999
                ,limits: [999999]
                ,height: 'full-110'
                ,done: function (res, curr, count){
                    tableData = res.data;
                    top.layer.close(shadeIndex);
                }
            })
        }
    });

    table.on('toolbar(table)', function(obj){
        var tableCheckData = table.checkStatus(obj.config.id);
        tableToolbarEnvent(obj);
        switch(obj.event){
            case 'save':
                if($("#month").val() == ''){
                    layer.msg('请先选择发生月！');
                    return;
                }
                if(tableData.length < 1){
                    layer.msg('当月没有设备折旧费！');
                    return;
                }
                for(var i in tableData){
                    tableData[i].disable = tableData[i].disable=='是'?true:false;
                    tableData[i].createUser = null;
                }
                var shadeIndex = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.4});
                $.ajax({
                    url: "/manage/elements_price/save",
                    type: "post",
                    data: JSON.stringify(tableData),
                    contentType:"application/json",
                    success: function (d){
                        if(d.code == '200'){
                            top.layer.msg("操作成功",{icon:1});
                        }else{
                            top.layer.msg("操作失败",{icon:2});
                        }
                        top.layer.close(shadeIndex);
                        table.reload('table');
                    },
                    error: function(){
                        top.layer.close(shadeIndex);
                        alert("操作失败！");
                    }
                });
                break;
            case 'custom_download':
                if($("#month").val() == ''){
                    layer.msg('请先选择发生月！');
                    return;
                }
                if(tableData.length < 1){
                    layer.msg('当月没有设备折旧费！');
                    return;
                }
                layer.confirm('确定要导出数据并下载到本地吗？', function (index) {
                    var conditionArray = new Array();
                    conditionArray.push({
                        field: "happen_month"
                        ,type: "equal"
                        ,value: $("#month").val()
                    });
                    if (top_dc != undefined) {
                        for (let i in top_dc.cacheCondition) {
                            if (top_dc.cacheCondition[i].conditionValueLeftVal.value != ''
                                || top_dc.cacheCondition[i].conditionValueRightVal.value != '' || top_dc.cacheCondition[i].conditionValueVal.value != '') {
                                var condition = {
                                    field: top_dc.cacheCondition[i].conditionFieldVal
                                    , type: top_dc.cacheCondition[i].conditionOptionVal
                                    , right: top_dc.cacheCondition[i].conditionValueRightVal.value
                                    , left: top_dc.cacheCondition[i].conditionValueLeftVal.value
                                    , value: top_dc.cacheCondition[i].conditionValueVal.value
                                }
                                conditionArray.push(condition);
                            }
                        }
                    }
                    window.location.href = "/manage/elements_price/download?url=cost_elements_price" + "&condition=" + encodeURI(JSON.stringify(conditionArray));
                    layer.close(index);
                });
                break;
        };
    });
})